package com.cloud.zhimao.system.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cloud.zhimao.common.api.QQInterConnection;
import com.cloud.zhimao.common.base.service.impl.IBaseServiceImpl;
import com.cloud.zhimao.common.config.exception.Assert;
import com.cloud.zhimao.common.utils.ServiceUtil;
import com.cloud.zhimao.system.user.dao.UserMapper;
import com.cloud.zhimao.system.user.pojo.entity.User;
import com.cloud.zhimao.system.user.pojo.vo.UserInfoVO;
import com.cloud.zhimao.system.user.service.UserService;
import com.qq.connect.javabeans.AccessToken;
import com.qq.connect.javabeans.qzone.UserInfoBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * @author Long
 */
@Slf4j
@Service
public class UserServiceImpl extends IBaseServiceImpl<User, Long, UserMapper> implements UserService {
    @Autowired
    private QQInterConnection qqInterConnection;

    @Override
    public UserInfoVO loginByQQ(HttpServletRequest req, HttpServletResponse res) {
        AccessToken accessToken = qqInterConnection.getAccessToken(req);
        String token = accessToken.getAccessToken();
        String openID = qqInterConnection.getOpenID(token);

        User user = new User(openID);
        if (ServiceUtil.isNotNull(this.queryByOpenID(user))){
            return this.getByUser(this.queryByOpenID(user));
        }

        UserInfoBean userInfoBean = qqInterConnection.getUserInfo(token, openID);
        Assert.notNull(userInfoBean, "授权信息获取失败");

        user.setNickname(userInfoBean.getNickname());
        user.setAvatar(userInfoBean.getAvatar().getAvatarURL30());

        Assert.isTrue(mapper.insert(user) > 0, "登录异常");

        this.setToekn(res, this.queryByOpenID(user));

        return this.getByUser(user);
    }

    @Override
    public User queryByOpenID(User user) {
        Assert.notNull(user.getOpenId(), "openId must be not null !");
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getOpenId, user.getOpenId());

        return mapper.selectOne(wrapper);
    }

    @Override
    public UserInfoVO getByUser(User user) {
        Assert.notNull(user, "user must be not null !");
        UserInfoVO userInfoVO = new UserInfoVO();
        BeanUtils.copyProperties(user, userInfoVO);
        return userInfoVO;
    }

    @Override
    public void setToekn(HttpServletResponse res, User user) {
        Assert.notNull(user, "user must be not null");
        String jwt = jwtUtils.generateToken(user.getId());
        res.setHeader("Authorization", jwt);
        res.setHeader("Access-Control-Expose-Headers", "Authorization");

        log.warn("==== TOKEN " + jwt);
    }

}
